<?php
namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\web\Request;

/**
 * 管理员日志
 *
 * Class ManagerLog
 * @package app\models
 *
 * @property integer $id
 * @property integer $mid
 * @property integer $time
 * @property string $ip
 * @property string $type
 * @property string $content
 *
 * @property Manager $manager 关联管理员
 */
class ManagerLog extends ActiveRecord
{

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ip', 'type'], 'string', 'max'=>32],
        ];
    }

    /**
     * 关联管理员
     * @return \yii\db\ActiveQuery
     */
    public function getManager()
    {
        return $this->hasOne(Manager::className(), ['id'=>'mid']);
    }

    /**
     * 记录日志
     *
     * @param integer $mid 管理员编号
     * @param string $type 事件类型
     * @param string $content 事件内容
     * @return boolean 是否保存成功
     */
    public static function info($mid, $type, $content)
    {
        $model = new ManagerLog();
        $model->mid = $mid;
        $model->time = time();
        $model->ip = Yii::$app->request instanceof Request ? Yii::$app->request->userIP : ''; // 如果命令行执行则没有IP
        $model->type = $type;
        $model->content = $content;
        return $model->save();
    }
}
